home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Libraries
/
SAT 2.3b4
/
Tutorial ƒ
/
Assignment6.p
< prev
next >
Wrap
Text File
|
1995-05-01
|
4KB
|
157 lines
program Assignment6;
uses
{$ifc UNDEFINED THINK_PASCAL}
Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, OSEvents, {}
{$endc}
SAT;
const
kSpeed = 5;
var
ignore: SpritePtr;
direction: Integer;
theSound: Handle;
score: Longint;
startTime: Longint;
doneFlag: Boolean;
procedure HandleSprite (me: SpritePtr);
var
event: EventRecord;
begin
{Now hold on to the hat! I'm using GetOSEvent to get key down events.}
{the keydowns then affect the speed variable in the sprite. That speed}
{is added to the position. Finally, I check the position against the screen}
{borders, and modify the speed in case I reach a border.}
if GetOSEvent(keyDownMask, event) then
if event.what = keyDown then
case char(BAnd(event.message, charCodeMask)) of
'a':
me^.speed.h := me^.speed.h - 1;
's':
me^.speed.h := me^.speed.h + 1;
'w':
me^.speed.v := me^.speed.v - 1;
'z':
me^.speed.v := me^.speed.v + 1;
end;
me^.position.h := me^.position.h + me^.speed.h;
me^.position.v := me^.position.v + me^.speed.v;
if me^.position.h < 0 then
begin
me^.speed.h := abs(me^.speed.h);
me^.position.h := 0;
end;
if me^.position.h > gSAT.offSizeH - 32 then
begin
me^.speed.h := -abs(me^.speed.h);
me^.position.h := gSAT.offSizeH - 32;
end;
if me^.position.v < 0 then
begin
me^.speed.v := abs(me^.speed.v);
me^.position.v := 0;
end;
if me^.position.v > gSAT.offSizeV - 32 then
begin
me^.speed.v := -abs(me^.speed.v);
me^.position.v := gSAT.offSizeV - 32;
end;
end; {HandleSprite}
procedure SetupSprite (me: SpritePtr);
begin
me^.task := @HandleSprite;
me^.face := SATGetFace(128);
me^.speed := Point(0);
SetRect(me^.hotRect, 0, 0, 32, 32);
end; {SetupSprite}
procedure SetupTarget (me: SpritePtr);
forward;
procedure HandleTarget (me: SpritePtr);
begin
{The target sprite isn't modified much. I just limit movement after gSAT.offSizeH instead}
{of a hard-coded constant.}
me^.position.h := me^.position.h + direction;
if me^.position.h <= 0 then
direction := kSpeed;
if me^.position.h >= gSAT.offSizeH - 32 then
direction := -kSpeed;
end; {HandleTarget}
procedure HitTarget (me, him: SpritePtr);
var
savePort: SATPort;
r: Rect;
begin
if him^.task = @HandleSprite then {Chack what we hit!}
begin
me^.task := nil;
ignore := SATNewSprite(-1, 0, SATRand(gSAT.offSizeV - 32), @SetupTarget);
{We could also re-use the old sprite for a new one, if we like.}
SATSoundPlay(theSound, 1, true);
{Add to the score}
score := score + 1;
{Draw the score on the screen.}
SATGetPort(savePort); {Save port and device!}
SATSetPortBackScreen;
SetRect(r, 100, 0, 100 + stringWidth(stringof('Caught: ', score : 1)), 15);
EraseRect(r);
MoveTo(r.left, r.bottom - 3);
DrawString(stringof('Caught: ', score : 1));
SATBackChanged(r);
SATSetPort(savePort); {Always restore!}
if score = 10 then
doneFlag := true;
end;
end; {HitTarget}
procedure SetupTarget (me: SpritePtr);
begin
me^.task := @HandleTarget;
me^.hitTask := @HitTarget;
me^.face := SATGetFace(129);
SetRect(me^.hotRect, 0, 0, 32, 32);
direction := kSpeed;
end; {SetupTarget}
const
kTicksPerFrame = 2;
var
t: Longint;
begin
{If we don't use Think Pascal, we must make standard inits ourselves.}
{$ifc UNDEFINED THINK_PASCAL}
SATInitToolbox;
{$endc}
SATConfigure(false, kVPositionSort, kForwardCollision, 32);
SATInit(128, 129, 478, 302);
ignore := SATNewSprite(0, 200, 200, @SetupSprite);
ignore := SATNewSprite(0, 0, SATRand(gSAT.offSizeV), @SetupTarget);
theSound := SATGetNamedSound('TestSound');
score := 0;
HideCursor;
startTime := TickCount;
doneFlag := false;
while not Button and not doneFlag do
begin
t := TickCount;
SATRun(true);
while TickCount < t + kTicksPerFrame do
;
end;
ShowCursor;
{Extremely simple result report. A real game should, of course, display this in the game}
{window, in a prettier way, perhaps with a high score list, etc.}
SATReportStr(stringof('Time to catch ', score : 1, ' disks: ', (TickCount - startTime) div 60 : 1, ' seconds.'));
SATSoundShutup;
end.